#include <list>
#include <iostream>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include "HttpConnection.h"
#include "HttpServer.h"

HttpServer::HttpServer(::boost::asio::io_context& ioc, const std::string& ip, unsigned short port) : 
	_ioc(ioc), _ip(ip), _port(port), _acceptor(_ioc, {::boost::asio::ip::make_address(_ip), _port})
{
	startAccept();
}

void HttpServer::listen()
{
	_ioc.run();
}

void HttpServer::startAccept()
{
	_acceptor.async_accept([&](::boost::beast::error_code ec, ::boost::asio::ip::tcp::socket socket)
	{
		if (ec)
			std::cout << "链接报错:" << ec.message() << std::endl;
		else
			std::make_shared<HttpConnection>(_ioc, std::move(socket))->start();

		startAccept();
	});
} 
